package com.ssm.pre.net;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.Connection.Response;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import com.ssm.pre.util.ToolUtil;
import com.ssm.util.Constants;


/**
 * 顶点小说网
 * @author 卫志强
 *
 */
public class GetNovelBookstoreNet {
	
	private static final String NOVEL_CLASSIFICATION_URL = "http://www.x23us.com/";//顶点小说网
	
	/**
	 * 顶点小说网---分类
	 */
	public static List<Map<String, Object>> getNovelClassification() throws Exception {
		List<Map<String,Object>> beans = new ArrayList<>();
		Response res = Jsoup.connect(NOVEL_CLASSIFICATION_URL).method(Connection.Method.GET)
				.header("User-Agent", Constants.USER_AGENT)
				.header("Upgrade-Insecure-Requests", "1").timeout(10000).execute();
		Document objectDoc = res.parse();
		Elements eles = objectDoc.select("li");
		for(Element ele : eles){
			if(ele.select("a").size()==1){
				if(ele.select("a").get(0).attr("href").indexOf("http://")==-1 && ele.select("a").get(0).html().length() == 4){
					Map<String,Object> bean = new HashMap<>();
					bean.put("id", ToolUtil.getSurFaceId());
					bean.put("webTitle", "顶点小说网");
					bean.put("novelTypeName", ele.select("a").get(0).html());
					bean.put("novelUrl", NOVEL_CLASSIFICATION_URL + ele.select("a").get(0).attr("href"));
					beans.add(bean);
				}
			}
		}
		return beans;
	}
	
	/**
	 * 顶点小说网---获取小说列表内容
	 */
	public static List<Map<String, Object>> getNovelMationByPage(String url, String page) throws Exception {
		List<Map<String,Object>> beans = new ArrayList<>();
		url = url.split("_")[0] + "_" + page + ".html";
		Response res = Jsoup.connect(url).method(Connection.Method.GET)
				.header("User-Agent", Constants.USER_AGENT)
				.header("Upgrade-Insecure-Requests", "1").timeout(10000).execute();
		Document objectDoc = res.parse();
		Elements eles = objectDoc.select("tr");
		for(Element ele : eles){
			if(ele.select("td").size() == 6){
				Map<String,Object> bean = new HashMap<>();
				bean.put("id", ToolUtil.getSurFaceId());
				String descUrl = ele.select("td").get(0).select("a").get(0).attr("href");
				Response res1 = Jsoup.connect(descUrl).method(Connection.Method.GET)
						.header("User-Agent", Constants.USER_AGENT)
						.header("Upgrade-Insecure-Requests", "1").timeout(10000).execute();
				Document objectDoc1 = res1.parse();
				Elements eles1 = objectDoc1.select("div .fl");
				for(Element ele1 : eles1){
					if(ele1.select("a").size() == 1){
						bean.put("imgUrl", NOVEL_CLASSIFICATION_URL + ele1.select("a").get(0).select("img").get(0).attr("src"));
					}
					if(ele1.select("table").size() == 1){
						bean.put("novelType", ele1.select("a").get(0).html());
					}
				}
				bean.put("novelCatalog", ele.select("td").get(0).select("a").get(1).attr("href"));
				bean.put("novelName", ele.select("td").get(0).select("a").get(1).html());
				bean.put("novelAuther", ele.select("td").get(2).html());
				bean.put("readType", ele.select("td").get(5).html());
				beans.add(bean);
			}
		}
		return beans;
	}
	
	/**
	 * 顶点小说网---获取小说章节列表
	 * url : 小说列表中的novelCatalog
	 * 		例如：http://www.x23us.com/html/64/64761/
	 */
	public static List<Map<String, Object>> getNovelChapterMationByUrl(String url, String novelId) throws Exception {
		List<Map<String,Object>> beans = new ArrayList<>();
		Response res = Jsoup.connect(url).method(Connection.Method.GET)
				.header("User-Agent", Constants.USER_AGENT)
				.header("Upgrade-Insecure-Requests", "1").timeout(10000).execute();
		Document objectDoc = res.parse();
		Elements eles = objectDoc.select("tbody");
		for(Element ele : eles){
			Elements eles1 = ele.select("a");
			for(Element ele1 : eles1){
				if(ele1.attr("href").indexOf("http://")==-1){
					Map<String,Object> bean = new HashMap<>();
					bean.put("id", ToolUtil.getSurFaceId());
					bean.put("novelId", novelId);
					bean.put("novelChapterName", ele1.html());
					bean.put("novelChapterUrl", url + ele1.attr("href"));
					beans.add(bean);
				}
			}
		}
		return beans;
	}
	
	/**
	 * 顶点小说网---获取小说内容
	 * url : 小说列表中的novelCatalog
	 * 		例如：http://www.x23us.com/html/64/64761/
	 */
	public static Map<String, Object> getNovelContentMationByUrl(String url, String novelChapterId) throws Exception {
		Map<String,Object> bean = new HashMap<>();
		Response res1 = Jsoup.connect(url).method(Connection.Method.GET)
				.header("User-Agent", Constants.USER_AGENT)
				.header("Upgrade-Insecure-Requests", "1").timeout(10000).execute();
		bean.put("novelContent", res1.parse().select("dd[id=contents]").get(0).html());
		bean.put("novelChapterId", novelChapterId);
		return bean;
	}
	
	
	
	
	public static void main(String[] args) throws Exception {
		System.out.println(getNovelContentMationByUrl("http://www.x23us.com/html/64/64761/26385610.html","406baebff0594e55ad6c7f466854fd5c"));
	}
	
}
